﻿#include "dialoginiprogram.h"
#include "ui_dialoginiprogram.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonParseError>

#include "Business/inputhandler.h"

#include "mainwindow.h"
#include "dialoglogin.h"
#include <QCloseEvent>

extern DialogIniProgram* iniProgram;
extern DialogLogin* login;
extern MainWindow* mainWindow;

extern QApplication* app;

DialogIniProgram::DialogIniProgram(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::DialogIniProgram)
{
    ui->setupUi(this);
    // 设置父窗口关闭时，此窗口也关闭
    this->setAttribute(Qt::WA_QuitOnClose, false);
}

DialogIniProgram::~DialogIniProgram()
{
    delete ui;
}

void DialogIniProgram::closeEvent(QCloseEvent *event)
{
    QJsonObject in, out;
    InputHandler::getInstance()["MainWindow::closeEvent"](in, out);

    emit closeProgram();
    event->accept();

}

void DialogIniProgram::on_btnHelp_clicked()
{
    QString title{"帮助"};
    QString content{"本软件使用aes-256进行本地加密存储，因此要先输入密钥来读取配置文件并初始化"};
    int ret = QMessageBox::information(this, title, content);
    if (ret == QMessageBox::Ok) {
        qDebug() << "点击确定";
    }
}


void DialogIniProgram::on_btnFromFile_clicked()
{

    QString filePath = QFileDialog::getOpenFileName(this,
                                               "选择要打开的密钥文件",
                                               QDir::currentPath(),
                                               "密钥文件(*.key);;所有文件(*.*)");

    ui->lineEditFilePath->setText(filePath);
}


void DialogIniProgram::on_btnOK_clicked()
{
    QString filePath = ui->lineEditFilePath->text();
    QJsonObject args_in, args_out;
    args_in.insert("AES256FilePath", filePath);

    InputHandler::getInstance()["DialogIniProgram::on_btnOK_clicked"](args_in, args_out);

    if (args_out["status"] == "true") {
        // 如果验证成功
        qDebug()<< "成功";

        mainWindow = new MainWindow();
        login = new DialogLogin();

        // 初始化整个程序
        QObject::connect(login, &DialogLogin::closeProgram, app, &QApplication::quit);
        QObject::connect(mainWindow, &MainWindow::closeProgram, app, &QApplication::quit);

        login->show();
        this->hide();


    } else {
        // 如果验证不成功
        qDebug()<< "失败";
        ui->labelShowText->setText("解析文件失败，将判断AES密钥是否正确");
    }

}


void DialogIniProgram::on_btnRegenerate_clicked()
{
    QString targetPath = QFileDialog::getExistingDirectory(this,
                                                           "选择要保存的目录",
                                                           QDir::currentPath());

    QJsonObject args_in, args_out;
    args_in.insert("targetPath", targetPath);

    InputHandler::getInstance()["DialogIniProgram::on_btnRegenerate_clicked"](args_in, args_out);

    if (args_out["status"] == "true") {
        ui->labelShowText->setText("密钥生成成功，请妥善保管！");
        ui->lineEditFilePath->setText(args_out["outputPath"].toString());
    }

}

